package frame;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import java.awt.Toolkit;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;

import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener; 
public class DoubleB {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DoubleB window = new DoubleB();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DoubleB() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(0, 0, 800, 630);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		
		
		Image image=new ImageIcon("resource\\chess.png").getImage();  
		//Image image2=new ImageIcon("resource\\黑娃.jpg").getImage();  
		JPanel panel = new BackgroundPanel(image); 
		
		//JPanel panel = new JPanel();
		panel.setBounds(0, 0, 600, 600);
		frame.getContentPane().add(panel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_1.setBounds(605, 0, 189, 602);
		frame.getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(65, 32, 54, 15);
		panel_1.add(lblNewLabel);
		panel.setLayout(null);
		
		
		//****************************************
		// TODO Auto-generated constructor stub  
        Container container = frame.getContentPane();  
        container.addMouseListener(new MouseListener() {  
  
            @Override  
            public void mouseClicked(MouseEvent e) {  
                // TODO Auto-generated method stub  
                System.out.print("单击了鼠标按键，");  
                int i = e.getButton();  
                if (i == MouseEvent.BUTTON1) { 
                    	
                	System.out.print("单击的是鼠标左键，");
                	Image image2=new ImageIcon("resource\\黑娃.png").getImage();  
                	JPanel panel = new ChessPanel(image2,e.getPoint().x,e.getPoint().y,1);
                	panel.setBounds(0, 0, 600, 600);
            		frame.getContentPane().add(panel);
                }
                if (i == MouseEvent.BUTTON2)  
                    System.out.print("单击的是鼠标中键，");  
                if (i == MouseEvent.BUTTON3)  
                    System.out.print("单击的是鼠标右键，");  
                int clickCount = e.getClickCount();  
                System.out.println("单击次数为" + clickCount + "下");  
            }  
  
            @Override  
            public void mousePressed(MouseEvent e) {  
                // TODO Auto-generated method stub  
                System.out.print("鼠标按键被按下，");  
                int i = e.getButton();  
                if (i == MouseEvent.BUTTON1)  
                    System.out.println("按下的是鼠标左键");  
                if (i == MouseEvent.BUTTON2)  
                    System.out.println("按下的是鼠标中键");  
                if (i == MouseEvent.BUTTON3)  
                    System.out.println("按下的是鼠标右键");  
            }  
  
            @Override  
            public void mouseReleased(MouseEvent e) {  
                // TODO Auto-generated method stub  
                System.out.print("鼠标按键被释放，");  
                int i = e.getButton();  
                if (i == MouseEvent.BUTTON1)  
                    System.out.println("释放的是鼠标左键");  
                if (i == MouseEvent.BUTTON2)  
                    System.out.println("释放的是鼠标中键");  
                if (i == MouseEvent.BUTTON3)  
                    System.out.println("释放的是鼠标右键");  
            }  
  
            @Override  
            public void mouseEntered(MouseEvent e) {  
                // TODO Auto-generated method stub  
                System.out.println("光标移入组件");  
            }  
  
            @Override  
            public void mouseExited(MouseEvent e) {  
                // TODO Auto-generated method stub  
                System.out.println("光标移出组件");  
            }  
            
        }); 
		
		
	}
	
	
	/** 
	 * 有背景图片的Panel类 
	 * @author tntxia 
	 * 
	 */  
	public class BackgroundPanel extends JPanel {  
	    /** 
	     *  
	     */  
	    private static final long serialVersionUID = -6352788025440244338L;  
	      
	    private Image image = null;  
	    //private int[][] chess;
	    public BackgroundPanel(Image image) {  
	        this.image = image;  
	    }  
	  
	    // 固定背景图片，允许这个JPanel可以在图片上添加其他组件  
	    protected void paintComponent(Graphics g) {  
	        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
	    }  
	    @Override
	    public void paint(Graphics arg0) {
	    	
	    	super.paint(arg0);
	    }
	    
	}
	/**
	 * 
	 * @author shaorong
	 *
	 */
	class ChessPanel extends JPanel  
	{  
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		Image im;
	    int posX;
	    int posY;
	    int color;
	    public ChessPanel(Image im,int posX,int posY,int color)  
	    {  
	        this.im=im;
	        this.posX=posX;
	        this.posY=posY;
	        this.color=color;
	        
	        this.setOpaque(true);  
	    }  
	    //Draw the back ground.  
	    public void paintComponent(Graphics g)  
	    {  
	        super.paintComponents(g);  
	        g.drawImage(im,posX,posY,30,30,this);  
	          
	    }  
	} 
}
